package com.app.micaihu.custom.view;

import android.content.Context;
import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatEditText;

import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

/**
 * Created by Guo on 2017/3/1.
 *
 *
 *
 * 限制字数的.不带表情的 edittext
 */

public class MaxByteLengthEditTextWithoutEmoji extends AppCompatEditText {

    private int maxByteLength = 10;//默认5个汉字.10个英文.提供set/get方法

    private String encoding = "GBK";//确认编码为GBK编码,汉字为一个 汉字 2个字节
    private int len = 0;

    //emoji正则
    Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
    private OnLimitedListener onLimitedListener;


    public interface OnLimitedListener{
        void onLimit(CharSequence source,int len);

        void onEmojiInput();
    }

    public void setOnLimitedListener(OnLimitedListener onLimitedListener){

        this.onLimitedListener = onLimitedListener;
    }

    public MaxByteLengthEditTextWithoutEmoji(Context context) {
        super(context);
        init();
    }

    public MaxByteLengthEditTextWithoutEmoji(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MaxByteLengthEditTextWithoutEmoji(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {

        setFilters(new InputFilter[]{inputFilter});


    }


    public int getMaxByteLength() {
        return maxByteLength;
    }

    public void setMaxByteLength(int maxByteLength) {
        this.maxByteLength = maxByteLength;
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }


    /**
     * input输入过滤
     */
    private InputFilter inputFilter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            try {
                if (emoji.matcher(source).find()) {
                    if(onLimitedListener != null){
                        onLimitedListener.onEmojiInput();
                    }
                    return "";
                }
                boolean more = false;
                do {
                    SpannableStringBuilder builder =
                            new SpannableStringBuilder(dest).replace(dstart, dend, source.subSequence(start, end));
                    len = builder.toString().getBytes(encoding).length;
                    more = len > maxByteLength;
                    if (more) {
                        end--;
                        source = source.subSequence(start, end);
                    }
                } while (more);

                if(onLimitedListener != null){
                    onLimitedListener.onLimit(source,len);
                }
                return source;
            } catch (UnsupportedEncodingException e) {
                return "Exception";
            }
        }
    };

}
